<?php

/**
 * This is the model class for table "{{search_log}}".
 *
 * The followings are the available columns in table '{{search_log}}':
 * @property integer $id
 * @property string $criteria
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class SearchLog extends ActiveRecord {
    
    public $criteria_;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{search_log}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id_);
        $criteria->compare('criteria', $this->criteria_, true);
        //$criteria->compare('created_by', $this->created_by);
        //$criteria->compare('created_date', $this->created_date, true);
        //$criteria->compare('modified_by', $this->modified_by);
        //$criteria->compare('modified_date', $this->modified_date, true);

        return new ActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Search the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    protected function afterSave() {
        parent::afterSave();
        $this->saveBrowserLog();
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->criteria=CJSON::encode($this->criteria_);
                $this->setCreateDateTime();
            } else {
                $this->setUpdateDateTime();
            }
            return true;
        }
        else
            return false;
    }
    
    public function addRecord() {
        $this->beginTransaction();
        try{
            if ($this->save(true)) {
                
            }
            if($this->hasErrors()){
                $this->rollback();
                return false;
            }else{
                $this->commit();
                $this->saveLog();
                return true;
            }
        } catch (Exception $ex) {
            $this->rollback();
        }
    }
    
    public function getCriteriaContent(){
        return CJSON::decode($this->criteria);
    }
    
    public function findAllCriteriaContents(){
        $t=$this->getTableAlias(false);
        $criteria = new CDbCriteria;
        $criteria->select = 'id,criteria';
        $criteria->distinct = true;
        $criteria->group = 'criteria';
        $criteria->addCondition(array(
            $t . '.criteria <> \'""\'',
        ));
        $model=$this->findAll($criteria);
        $option= CHtml::listData($model, function($model){ return $model->id; }, function($model){ return $model->criteria; });

        return $option;   
    }
}
